// pages/zhuajiu_room/index.js
var request = require('../../helpers/request.js')
var config = require('../../config.js')
var alert = require('../../helpers/alert.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    room: null,
    seats: [],//以座位信息为单位显示玩家信息，准备状态等，每次请求roomInfo后更新

    pageActive: true,//是否持续刷新
    requestDelay: 2000,//持续刷新间隔
    ready: false,//是否准备好
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log('页面准备好了')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('打开页面')

    //获取全局变量中保存的初始房间信息，用于初始化显示
    //无论创建还是进入房间都要准备好这个信息
    var room = wx.getStorageSync('room')
    if (!room) {
      wx.navigateBack({
        delta: 1
      })
      return
    }
    this.setData({
      room,
      pageActive: true
    })
    this.render(room)


    //重复请求房间信息
    this.loopRequestRoomInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
      console.log('页面隐藏')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('页面卸载')
    this.setData({
      pageActive: false
    })

    //Exit room
    request.request(
      config.service.zhuajiu + '/exitRoom',
      'GET',
      null,
      res => {
        var data = res.data
        console.log(data)
      },
      err => {
        alert.showWarn('请求Exit room失败')
      }
    )
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 准备开始
   */
  ready: function(){
    var me = this
    request.request(
      config.service.zhuajiu + '/ready',
      'GET',
      null,
      res => {
        var data = res.data
        console.log(data)
        me.setData({
          ready: true
        })
      },
      err => {
        alert.showWarn('准备失败')
        me.setData({
          ready: false
        })
      }
    )
  },

  unReady: function(){
    var me = this
    request.request(
      config.service.zhuajiu + '/unReady',
      'GET',
      null,
      res => {
        var data = res.data
        console.log(data)
        me.setData({
          ready: false
        })
      },
      err => {
        alert.showWarn('取消准备失败')
        me.setData({
          ready: true
        })
      }
    )
  },

  //循环获取房间信息
  loopRequestRoomInfo: function(){
    var me = this
    
    function requestRoomInfo(){
      if(!me.data.pageActive) //页面被卸载的情况下直接退出请求
        return

      request.request(
        config.service.zhuajiu + '/getRoomInfo',
        'GET',
        null,
        res => {
          var data = res.data
          console.log(data)

          if (data.result != 'success')
            return

          var room = data.data
          wx.setStorageSync('room', room)

          //处理实时信息
          me.processRoomInfo(room)

          //一秒重新请求一次
          setTimeout(requestRoomInfo, me.data.requestDelay)
        },
        err => {
          alert.showWarn('请求失败')
        }
      )
    }

    requestRoomInfo()
  },

  //以座位信息为单位更新显示
  render: function(room){
    var seats = []

    //Format
    for(var people of room.peoples){
      seats.push(people)
    }
    for(var i=seats.length;i<room.max;i++){
      seats.push({
        name: '空'
      })
    }

    this.setData({
      seats
    })
  },

  //处理实时请求获取的房间信息
  processRoomInfo(room){
    var me = this

    //渲染seats
    me.render(room)

    //如果有结果就要显示结果
    if(room.results && room.results.length > 0){
      console.log('获取抽签结果')
      var msg = ''
      for(var i=0;i<room.peoples.length;i++){
        msg += (room.peoples[i].name + ' : ' + room.results[i].data+', ')
      }
      // alert.showWarn(msg)
      wx.showModal({
        title: '抽签结果',
        content: msg,
      })
    }
  }
})